import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class TestApp
{
    private static final int port = 21000;
    
    public static void main(String[] args)
    {
        if (args[0].equals("-s"))
            server();
        else
            client(args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]));
    }
    
    private static void server()
    {
        try
        {
            ServerSocket serverSoc = new ServerSocket(port);
            while (true)
            {
                Socket soc = serverSoc.accept();
                new Thread(new Worker(soc)).start();
            }
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private static class Worker implements Runnable
    {
        Socket soc;
        
        Worker(Socket soc)
        {
            this.soc = soc;
        }

        @Override
        public void run()
        {
            try
            {
                ObjectInputStream in = new ObjectInputStream(soc.getInputStream());
                ObjectOutputStream out = new ObjectOutputStream(soc.getOutputStream());
                
                int a = in.readInt();
                //int b = in.readInt();
                int option = in.readInt();
                if (option == 1)
                    out.writeInt(factorial(a));
                else if (option == 2)
                    out.writeInt(fibonacci(a));
                
                out.flush();
                
                in.close();
                out.close();
                soc.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    private static int factorial (int n){
        if (n > 1)
            return factorial(n-1)*n;
        else
            return 1;
    }
    
    private static int fibonacci (int n){
        if (n <= 1)
            return n;
        else
            return fibonacci(n-1) + fibonacci(n-2);
    }
    
    private static void client(String host, int a, int option)
    {
        try
        {
            
            Socket soc = new Socket(host, port);

            ObjectOutputStream out = new ObjectOutputStream(soc.getOutputStream());
            
            out.writeInt(a);
            //out.writeInt(b);
            out.writeInt(option);
            
            out.flush();
            ObjectInputStream in = new ObjectInputStream(soc.getInputStream());
            int result = in.readInt();
            System.out.println("Opition is " + option);
            System.out.println(result);
            in.close();
            out.close();
            soc.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
